package org.unigram.oluolu.recommend;

import java.io.IOException;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;

/**
 * Add queries information. 
 */
public class AddFeaturesMapper extends
        Mapper<LongWritable, Text, LongWritable, Text> {

    /**
     * map function.
     * 
     * @param dummy not used
     * @param value related user ids pair or user id with the queries
     * @param context write down key and value
     * 
     * @throws IOException -
     * @throws InterruptedException -
     * @throws InterruptedException -
     */
    @Override
    public final void map(final LongWritable dummy,
            final Text value, final Context context)
            throws IOException, InterruptedException {
        
        String inputStr = value.toString();        
        try {
            String [] inputArray = inputStr.split("\t");
            
            if (inputStr.indexOf(":") > 0) {
                context.write(
                        new LongWritable(Long.parseLong(inputArray[0])), 
                        new Text(inputArray[1]));                
            } else {  // write candidate -> target
                context.write(
                        new LongWritable(Long.parseLong(inputArray[1])), 
                        new Text(inputArray[0]));
            }
            

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("PARSING ERROR in line: " + inputStr);
            e.printStackTrace();
        }        
     }    
}
